#include "head.h"
#include "func.h"


extern int a_flag;
extern int l_flag;
extern int flag_of_mulDir;
extern struct winsize sz;
struct dirent **fileList;

int link_len;       // 链接数长度
int uname_len;      // 用户长度
int gname_len;      // 用户组长度
int size_len;       // 文件大小长度
int filename_len;   // 文件名的最大长度

void ls_file(char *fileName) {
    if (l_flag) {
        printDetail(fileName);
    }
    printf("%s\n", fileName);
} 

void ls_dir(char *dirName) {
    int fileCnt;
    if ((fileCnt = scandir(dirName, &fileList, NULL, alphasort)) < 0) {
        char tmp[128];
        sprintf(tmp, "ls : %s", dirName);
        perror(tmp);
        exit(10);
    }
    if (l_flag) {
        // 获取连接数,用户名,组名,文件size的最大长度
        for (int i = 0; i < fileCnt; i++) {
            char allDirName[2048];
            getAllDir(dirName, fileList[i], allDirName);
            strCnt(allDirName);
        }
    } else {
        // 只要获取文件名的最大长度即可
        for (int i = 0; i < fileCnt; i++) {
            if (filename_len < strlen(fileList[i]->d_name)) {
                filename_len = strlen(fileList[i]->d_name);
            }
        }
    }
    // if (flag_of_mulDir) {
    //     printf("%s:\n", dirName);
    // }
    for (int i = 0; i < fileCnt; i++) {
        // -a 对应
        if (fileList[i]->d_name[0] == '.' && !a_flag) {
            continue;
        }
        if (l_flag) {
            i && printf("\n");
            char allDirName[2048];
            getAllDir(dirName, fileList[i], allDirName);
            printDetail(allDirName);
            printf("%s", fileList[i]->d_name);
        } else {
            printf("%s\n", fileList[i]->d_name);
            printByCol();
        }
        
    } 
    printf("\n");
    link_len = 0;
    size_len = 0;
    uname_len = 0;
    gname_len = 0;
}

void printDetail(char *name) {
    struct stat fileInfo;
    if (lstat(name, &fileInfo) < 0) {
        char tmp[128];
        sprintf(tmp, "ls : %s", name);
        perror(tmp);
        exit(11);
    }
    // 打印文件种类
        if(S_ISREG(fileInfo.st_mode))
            printf("-");
        else if(S_ISDIR(fileInfo.st_mode))
            printf("d");
        else if(S_ISCHR(fileInfo.st_mode))
            printf("c");
        else if(S_ISBLK(fileInfo.st_mode))
            printf("b");
        else if(S_ISFIFO(fileInfo.st_mode))
            printf("p");
        else if(S_ISLNK(fileInfo.st_mode)) {
            printf("l");
            // TODO:将软连接指向的文件路径跟在后面
        }
        else if(S_ISSOCK(fileInfo.st_mode))
            printf("s");
        else
            printf("error");

    // 打印权限
    (fileInfo.st_mode & S_IRUSR) ? printf("r") : printf("-");
    (fileInfo.st_mode & S_IWUSR) ? printf("w") : printf("-");
    (fileInfo.st_mode & S_IXUSR) ? printf("x") : printf("-");
    (fileInfo.st_mode & S_IRGRP) ? printf("r") : printf("-");
    (fileInfo.st_mode & S_IWGRP) ? printf("w") : printf("-");
    (fileInfo.st_mode & S_IXGRP) ? printf("x") : printf("-");
    (fileInfo.st_mode & S_IROTH) ? printf("r") : printf("-");
    (fileInfo.st_mode & S_IWOTH) ? printf("w") : printf("-");
    (fileInfo.st_mode & S_IXOTH) ? printf("x") : printf("-");
    printf(" ");
    // 输出链接数,用户名,组名,size
    printf("%*lu ", link_len, fileInfo.st_nlink);
    printf("%*s ", uname_len, getpwuid(fileInfo.st_uid)->pw_name);
    printf("%*s  ", gname_len, getgrgid(fileInfo.st_gid)->gr_name);
    printf("%*ld ", size_len, fileInfo.st_size);
    // 输出时间
    struct tm t;
    char date_time[64];
    // strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&fileInfo->st_ctim.tv_sec, &t));
    strftime(date_time, sizeof(date_time), "%b  %d %H:%M", localtime_r(&fileInfo.st_ctim.tv_sec, &t));
    printf("%s ", date_time);
}

void strCnt(char *fileName) {
    struct stat st;
    if (lstat(fileName, &st) < 0) {
        // printf(RED("Error_strCnt, %s\n"), fileName);
        // return -1;
        char tmp[128];
        sprintf(tmp, "ls : %s", fileName);
        perror(tmp);
        exit(12);
    }
    // 链接数长度
    int tmpLen = 0;
    int tmp = st.st_nlink;
    while (tmp) {
        tmp /= 10;
        tmpLen++;
    }
    if (link_len < tmpLen)
        link_len = tmpLen;
    // 用户长度
    char userName[1024];
    strcpy(userName, getpwuid(st.st_uid)->pw_name);
    tmpLen = strlen(userName);
    if (uname_len < tmpLen)
        uname_len = tmpLen;
    // 用户组长度
    tmpLen = strlen(getgrgid(st.st_gid)->gr_name);
    if (gname_len < tmpLen)
        gname_len = tmpLen;
    // 文件大小长度
    tmp = st.st_size;
    tmpLen = 0;
    while (tmp) {
        tmp /= 10;
        tmpLen++;
    }
    if (size_len < tmpLen)
        size_len = tmpLen;
}

int getAllDir(char *dirName, struct dirent *entry, char *allDir) {
    strcpy(allDir, dirName);
    if (dirName[strlen(dirName) - 1] != '/') {
        strcat(allDir, "/");
    }
    strcat(allDir, entry->d_name);
    return 0;
}

void printByCol() {
    int colCnt = ceil((double)sz.ws_col / filename_len);
}